<template>
  <div class="flex-wrapper">
    <div class="public-pagination">
      <div
        class="page pointer"
        @click="goPrePage"
        :class="{ noneClik: currentPage == 1 }"
      >
        «
      </div>
      <ul class="page-list">
        <li class="page-item" v-for="(page, index) in showPageList" :key="index">
          <div
            v-if="page"
            @click="goPage(page)"
            class="notPointer pointer"
            :class="{ active: page === currentPage }"
          >
            {{ page }}
          </div>
          <div v-else class="notPointer2">...</div>
        </li>
      </ul>
      <div
        class="page pointer"
        @click="goNextPage"
        :class="{ noneClik: currentPage == totalPage }"
      >
        »
      </div>
    </div>
  </div>
</template>

<script>
import { computed, defineComponent, reactive, toRefs } from "vue";

export default defineComponent({
  props: {
    page: {
      type: Object,
      require: true,
    },

  },
  setup(props,context) {
    const data = reactive({
      currentPage: 1,
    });

    let totalPage = computed(() => Math.ceil(props.page.total / props.page.pageSize));
    let showPageList = computed(() => {
      let pageNum = totalPage.value,
        index = data.currentPage,
        arr = [];

      if (pageNum <= 11) {
        for (let i = 1; i <= pageNum; i++) {
          arr.push(i);
        }
        return arr;
      }

      if (index <= 6)
        return [1, 2, 3, 4, 5, 6, 7, 0, pageNum - 2, pageNum - 1, pageNum];

      if (index >= 7 && index <= pageNum - 4) {
        return [
          1,
          0,
          index - 3,
          index - 2,
          index - 1,
          index,
          index + 1,
          index + 2,
          index + 3,
          0,
          pageNum,
        ];
      }

      if (index === pageNum - 2 || index === pageNum - 1 || index === pageNum) {
        return [
          1,
          2,
          3,
          4,
          5,
          6,
          0,
          pageNum - 3,
          pageNum - 2,
          pageNum - 1,
          pageNum,
        ];
      }

      return [
        1,
        2,
        3,
        4,
        5,
        0,
        pageNum - 4,
        pageNum - 3,
        pageNum - 2,
        pageNum - 1,
        pageNum,
      ];
    });

    const goPrePage = () => {
      if (data.currentPage == 1) return;
      data.currentPage -= 1;
      context.emit('changePage',data.currentPage)
    };

    const goNextPage = () => {
      if (data.currentPage == totalPage.value) return;
      data.currentPage += 1;
      context.emit('changePage',data.currentPage)
    };

    const goPage = (offset) => {
      data.currentPage = offset;
      context.emit('changePage',data.currentPage)
    };

    return {
      ...toRefs(data),
      totalPage,
      showPageList,
      goPrePage,
      goNextPage,
      goPage,
    };
  },
});
</script>


<style rel="stylesheet/scss" lang="scss" scoped>
//分页
.flex-wrapper {
  display: flex;
  justify-content: center;
}
.public-pagination {
  height: 24px;
  margin: 30px auto;
  text-align: center;
  display: flex;
  .page {
    width: 30px;
    height: 30px;
    background: #ffffff;
    border: 1px solid #cccccc;
    font-size: 14px;
    line-height: 28px;
    text-align: center;
    color: #696969;
    box-sizing: border-box;
    &:hover {
      background: #47b3e3;
      color: #fff;
      border: none;
    }
    &.disabled {
      &:hover {
        color: #c0c4cc;
        cursor: not-allowed;
      }
    }
  }
  .noneClik {
    cursor: not-allowed;
  }
  .page-list {
    display: inline-block;
    margin: 0 16px;
    font-size: 0;
    line-height: 0;
    padding-left: 0;
    .page-item {
      display: inline-block;
      margin-right: 16px;

      &:last-child {
        margin-right: 0;
      }
      .notPointer {
        display: block;
        font-size: 14px;
        line-height: 30px;
        width: 30px;
        height: 30px;
        color: #696969;
        text-align: center;
        border: 1px solid #cccccc;
        border-radius: 1px;
        min-width: 12px;
        &.active {
          background: #47b3e3;
          color: #fff;
          border: 1px solid transparent;
        }
        &:hover {
          background: #47b3e3;
          color: #fff;
          border: 1px solid transparent;
        }
        &.active-link {
          color: #fff;
          background: #bf1a2c;
          border-color: #bf1a2c;
        }
      }
      .notPointer2 {
        display: block;
        font-size: 14px;
        line-height: 30px;
        height: 30px;
        color: #696969;
        text-align: center;
        border-radius: 1px;
        min-width: 12px;
      }
    }
  }
}
</style>